<!--
 * @Author: Aaron
 * @Date: 2022-05-10 17:57:13
 * @LastEditors: Aaron Huang
 * @LastEditTime: 2022-09-15 17:50:28
 * @Description: file content
 * @FilePath: \ias_ui\src\views\iframe\index.vue
 * @Company: 沃尔玛
-->
<template>
  <iframe v-if="iframeSrc" style="position: absolute; left: 0; top: 0; width: 100%; height: 100%" :src="iframeSrc" frameborder="0" />
</template>

<script>
// eslint-disable no-unused-vars
import store from '@/store'
export default {
  name: 'Iframe',
  data() {
    return {
      iframeSrc: ''
    }
  },
  created() {
    const {
      path,
      meta: { baseUrl, params }
    } = this.$route
    const { token, userId, userName } = store.getters
    const APPID = store.state.user.appIdLogin
    const appName = window.appConfig.appName
    const data = { token, userId, userName, APPID, appName }
    const paramsStr = params.map((_) => `${_}=${data[_]}`).join('&')
    this.iframeSrc = `${baseUrl}${path}?fullscreen=true&t=${+new Date()}&${paramsStr}`
    window.addEventListener('message', this.onPostMessage, false)
  },
  beforeDestroy() {
    window.removeEventListener('message', this.onPostMessage)
  },
  methods: {
    onPostMessage(e) {
      const {
        meta: { baseUrl }
      } = this.$route
      if (baseUrl.startsWith(e.origin) && e.data === 'logout') {
        store.dispatch('FedLogOut').then(() => {
          location.reload()
        })
      }
    }
  }
}
</script>
